package com.cjw.util;

import com.cjw.enums.ErrorCode;
import com.cjw.exceptions.BusinessException;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.regex.Pattern;


@Getter
@NoArgsConstructor
@Embeddable
public class Telephone {

    /**
     * 验证码有效时间
     */
    public static final Long smsCodeExpireSecods = 5 * 60L;
    /**
     * 手机号码正则表达式
     */
    private static final String telephoneSentence = "^1\\d{13}";

    @Column(name = "telephone", unique = true)
    private String telephone;

    public Telephone(String telephone) {
        if (!StringUtils.isEmpty(telephone) && Pattern.matches(telephone, telephoneSentence)) {
            throw new BusinessException(ErrorCode.PHONE_NOT_LAWFUL);
        }
        this.telephone = telephone;
    }


    /**
     * 生成短信验证码
     *
     * @return
     */
    public static String generateSmsCode() {
        return RandomCodeUtil.getRandom6NumCode();
    }
}
